/*
 * Copyright (C) 2011 Mette Bank, Rikke Jensen, Kenneth Brodersen, Thomas Pedersen
 * 
 * This file is part of digiPECS.
 * 
 * digiPECS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * digiPECS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with digiPECS.  If not, see <http://www.gnu.org/licenses/>.
 */

package sw6.digipecs.admin;

import java.util.ArrayList;

import sw6.digipecs.R;
import sw6.digipecs.exceptions.IDNotFoundException;
import sw6.digipecs.exceptions.ImageIsEmptyException;
import sw6.digipecs.helpers.ToastHelper;
import sw6.digipecs.model.ExtImage;
import sw6.digipecs.model.ExtImageInSituation;
import sw6.digipecs.model.Situation;
import sw6.digipecs.widget.admin.SelectableExtImageAdapter;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class RemoveImagesFromSituationActivity extends Activity implements
		OnItemClickListener {
	public static final String SITUATION_ID = "situation_id";
	public static final int REQUEST_CODE = 555;
	private Intent mInvokeIntent;
	private int mSituationId;
	private Context mContext;
	private Situation mSituation = null;
	private GridView mImagesGrid;
	private ArrayList<ExtImageInSituation> mImagesList;
	private SelectableExtImageAdapter<ExtImageInSituation> mAdapter;

	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.deleteimagesfromsituationactivitylayout);
		Initialise();
	}

	private void Initialise() {
		mInvokeIntent = getIntent();
		mSituationId = (mInvokeIntent.getExtras()).getInt(SITUATION_ID);
		mContext = getApplicationContext();
		try {
			mSituation = new Situation(mContext, mSituationId);
		} catch (IDNotFoundException e) {
			ToastHelper.showError(mContext, R.string.errorNoSituation);
			e.printStackTrace();
		}
		mImagesGrid = (GridView) findViewById(R.id.deleteImagesFromSituationGridView);
		mImagesGrid.setOnItemClickListener(this);
		mImagesList = mSituation.getAllPictures(mContext);
		mAdapter = new SelectableExtImageAdapter<ExtImageInSituation>(mContext, mImagesList,
				SelectableExtImageAdapter.SELECT_MODE_DELETE);
		mImagesGrid.setAdapter(mAdapter);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		ExtImageInSituation image = mImagesList.get(arg2);
		image.setIsSelected(!image.isSelected());
		mImagesList.set(arg2, image);
		mAdapter.notifyDataSetChanged();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
	        deleteSelectedPictures(); 
	        setResult(RESULT_OK); 
	        finish(); 
	    }
	    return super.onKeyDown(keyCode, event);
	}

	private void deleteSelectedPictures() {
		for (ExtImage image : mImagesList) {
			if (image.isSelected()) {
				try {
					mSituation.deletePicture(image);
				} catch (ImageIsEmptyException e) {
					throw new RuntimeException(e);
				}
			}
		}
	}
}
